/**
 * Copyright (c) 2014, 2017, Oracle and/or its affiliates.
 * The Universal Permissive License (UPL), Version 1.0
 */
"use strict";
define(["ojs/ojcore","jquery","ojs/ojdatasource-common"],function(a){a.ay=function(){this.id=null;this.depth=0;this.parent=null;this.children=[];this.EZ=this.attr=this.title=null};a.ay.prototype.dqa=function(a){return function(b,c){return null!=b.attr&&null!=c.attr&&null!=b.attr[a]&&null!=c.attr[a]?b.attr[a]<c.attr[a]?-1:b.attr[a]===c.attr[a]?0:1:b[a]<c[a]?-1:b[a]===c[a]?0:1}};a.ay.prototype.Osa=function(a){return function(b,c){return null!=b.attr&&null!=c.attr&&null!=b.attr[a]&&null!=c.attr[a]?b.attr[a]<
c.attr[a]?1:b.attr[a]===c.attr[a]?0:-1:b[a]<c[a]?1:b[a]===c[a]?0:-1}};a.ay.prototype.Oda=function(a){var b=a.key;"ascending"===a.direction?this.children.sort(this.dqa(b)):"descending"===a.direction&&this.children.sort(this.Osa(b));for(var b=0,c=this.children.length;b<c;b++)this.children[b].Oda(a)};a.ud=function(g){var b;b=new a.ay;null==g.id&&(b.id="root");this.data=this.a6({count:0},b,g);a.ud.u.constructor.call(this,b)};o_("JsonTreeDataSource",a.ud,a);a.b.sa(a.ud,a.Iu,"oj.JsonTreeDataSource");a.ud.prototype.Init=
function(){a.ud.u.Init.call(this)};a.b.g("JsonTreeDataSource.prototype.Init",{Init:a.ud.prototype.Init});a.ud.prototype.a6=function(g,b,c,d){var e,f,h,k,l,m,r;null==d&&(d=0);for(k in c)if("children"==k||0==d&&c instanceof Array)for(e=0==d&&c instanceof Array?c:c[k],d++,r=0;r<e.length;r++){h=e[r];f=new a.ay;null==h.id&&(g.count++,null==h.attr?f.id="rid_"+g.count:null==h.attr.id&&(h.attr.id="rid_"+g.count));for(l in h)for(m in f)l==m&&"children"!=l&&(f[m]=h[l]),"depth"==m&&(f[m]=d);b.children.push(f);
for(m in h)"children"==m&&this.a6(g,b.children[r],h,d)}return b};a.ud.prototype.getChildCount=function(a){null==a&&(a=this.data.id);a=this.gt(this.data,a);return a.children?a.children.length:0};a.b.g("JsonTreeDataSource.prototype.getChildCount",{getChildCount:a.ud.prototype.getChildCount});a.ud.prototype.fetchChildren=function(g,b,c){var d,e,f,h,k;f=[];null==g&&(g=this.data.id);h=this.gt(this.data,g);b||(b=[],b.start=0,b.count=h.children.length);b.count||(b.count=h.children.length);b.start||(b.start=
0);d=b.start;e=Math.min(h.children.length,d+b.count);for(b=d;b<e;b+=1)k=new a.ay,null!=h.children[b].attr&&(k.attr=h.children[b].attr),null!=h.children[b].id&&(k.id=h.children[b].id),null!=h.children[b].depth&&(k.depth=h.children[b].depth),null!=h.children[b].title&&(k.title=h.children[b].title),null!=h.children[b].parent&&(k.parent=h.children[b].parent),k.EZ=0<h.children[b].children.length?!1:!0,f.push(k);g=new a.ih(d,e,f,g);null!=c&&null!=c.success&&c.success.call(null,g)};a.b.g("JsonTreeDataSource.prototype.fetchChildren",
{fetchChildren:a.ud.prototype.fetchChildren});a.ud.prototype.fetchDescendants=function(g,b){var c,d,e,f;e=[];null==g&&(g=this.data.id);f=this.gt(this.data,g);c=[];c.start=0;c.count=f.children.length;d=c.start;for(c=Math.min(f.children.length,d+c.count);d<c;d+=1)f.children[d].EZ=0<f.children[d].children.length?!1:!0,e.push(f.children[d]);e=new a.ih(0,e.length,e,g);null!=b&&null!=b.success&&b.success.call(null,e)};a.b.g("JsonTreeDataSource.prototype.fetchDescendants",{fetchDescendants:a.ud.prototype.fetchDescendants});
a.ud.prototype.moveOK=function(){return"valid"};a.b.g("JsonTreeDataSource.prototype.moveOK",{moveOK:a.ud.prototype.moveOK});a.ud.prototype.move=function(g,b,c,d){var e;e=b;if(null==e||e==this.data.id){if("inside"!=c){a.t.log("Error: root can not be the reference node if position equals to "+c);return}e||(e=this.data.id)}g=this.gt(null,g);this.gt(g,e)?a.t.log("Error: the node to move contains the reference node as its sub-tree."):(b=this.gt(null,e),e=this.PT(e),this.tCa(g),"inside"==c?(this.Tq(g,g.depth-
(b.depth+1)),b.children.push(g)):"before"==c?(this.Tq(g,g.depth-b.depth),c=e.children.indexOf(b),-1<c&&(0!=c?e.children.splice(c,0,g):e.children.unshift(g))):"after"==c?(this.Tq(g,g.depth-b.depth),c=e.children.indexOf(b),-1<c&&e.children.splice(c+1,0,g)):"first"==c?(this.Tq(g,g.depth-b.depth),e.children.unshift(g)):"last"==c&&(this.Tq(g,g.depth-b.depth),e.children.push(g)),null!=d&&null!=d.success&&d.success.call(null,this.data))};a.b.g("JsonTreeDataSource.prototype.move",{move:a.ud.prototype.move});
a.ud.prototype.sort=function(a,b){var c;c=this.gt(this.data,this.data.id);c.Oda(a);null!=b&&null!=b.success&&b.success.call(null,c)};a.b.g("JsonTreeDataSource.prototype.sort",{sort:a.ud.prototype.sort});a.ud.prototype.getSortCriteria=function(){return{key:null,direction:"none"}};a.b.g("JsonTreeDataSource.prototype.getSortCriteria",{getSortCriteria:a.ud.prototype.getSortCriteria});a.ud.prototype.PT=function(a,b){var c,d=null;if(a==this.data.id)return null;null==b&&(b=this.data);if(b.children&&0<b.children.length){for(c=
0;c<b.children.length;c++)if(b.children[c].id&&b.children[c].id==a||b.children[c].attr&&b.children[c].attr.id==a)return b;for(c=0;c<b.children.length&&!(d=this.PT(a,b.children[c]));c++);}return d};a.ud.prototype.gt=function(a,b){var c,d=null;null==a&&(a=this.data);if(a.id&&a.id==b||a.attr&&a.attr.id==b)return a;if(null!=a.children)for(c=0;c<a.children.length&&!d;c++)d=a.children[c].id&&a.children[c].id==b||a.children[c].attr&&a.children[c].attr.id==b?a.children[c]:this.gt(a.children[c],b);return d};
a.ud.prototype.Tq=function(a,b){var c;a.depth-=b;if(a.children&&0!=a.children.length)for(c=0;c<a.children.length;c++)this.Tq(a.children[c],b)};a.ud.prototype.tCa=function(a){var b;null!=a.id?b=a.id:null!=a.attr&&(b=a.attr.id);(b=this.PT(b))||(b=this.data);a=b.children.indexOf(a);-1<a&&b.children.splice(a,1)};a.ud.prototype.getCapability=function(a){return"fetchDescendants"===a?"enable":"sort"===a?"default":"batchFetch"===a?"disable":"move"===a?"full":null};a.b.g("JsonTreeDataSource.prototype.getCapability",
{getCapability:a.ud.prototype.getCapability});a.ih=function(g,b,c,d){a.p.Uq(g,null);a.p.Uq(b,null);this.cKa=d;this.Wt=g;this.CN=b;this.Bi=c};o_("JsonNodeSet",a.ih,a);a.ih.prototype.getParent=function(){return this.cKa};a.b.g("JsonNodeSet.prototype.getParent",{getParent:a.ih.prototype.getParent});a.ih.prototype.getStart=function(){return this.Wt};a.b.g("JsonNodeSet.prototype.getStart",{getStart:a.ih.prototype.getStart});a.ih.prototype.getCount=function(){return Math.max(0,this.CN-this.Wt)};a.b.g("JsonNodeSet.prototype.getCount",
{getCount:a.ih.prototype.getCount});a.ih.prototype.getData=function(g){a.p.assert(g<=this.CN&&g>=this.Wt);g-=this.Wt;return this.Bi[g]?this.Bi[g].attr:null};a.b.g("JsonNodeSet.prototype.getData",{getData:a.ih.prototype.getData});a.ih.prototype.getMetadata=function(g){var b=[];a.p.assert(g<=this.CN&&g>=this.Wt);g-=this.Wt;b.key=this.Bi[g].id?this.Bi[g].id:this.Bi[g].attr.id;b.leaf=this.Bi[g].EZ;b.depth=this.Bi[g].depth;null==b.leaf&&(b.leaf=this.Bi[g].children&&0<this.Bi[g].children.length?!1:!0);
return b};a.b.g("JsonNodeSet.prototype.getMetadata",{getMetadata:a.ih.prototype.getMetadata});a.ih.prototype.Tq=function(a,b){var c;b++;a.depth=b;if(a.children&&0!=a.children.length)for(c=0;c<a.children.length;c++)this.Tq(a.children[c],b)};a.ih.prototype.ri=function(g){var b,c,d;a.p.assert(g<=this.CN&&g>=this.Wt);g-=this.Wt;c=this.Bi[g].depth;b=this.Bi[g].children;if(0==b.length)return null;g=this.Bi[g].id?this.Bi[g].id:this.Bi[g].attr.id;for(d=0;d<b.length;d++)this.Tq(b[d],c);return new a.ih(0,b.length,
b,g)};a.b.g("JsonNodeSet.prototype.getChildNodeSet",{ri:a.ih.prototype.ri})});